package club.orqlog.schedule.elasticjob;

import club.orqlog.schedule.elasticjob.util.JdbcUtil;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;

import java.util.List;
import java.util.Map;

public class ArchiveJob implements SimpleJob {

    @Override
    public void execute(ShardingContext shardingContext) {
        int shardingItem = shardingContext.getShardingItem();
        System.out.println(">>>>> 当前分片: " + shardingItem);
        String shardingParameter = shardingContext.getShardingParameter(); // 0=大专,1=本科,2=研究生

        String selectSql = "select * from schedule_resume where state = '未归档' AND education = '" + shardingParameter +"' limit 1";
        List<Map<String, Object>> list = JdbcUtil.executeQuery(selectSql);

        if (list == null || list.size() == 0) {
            return;
        }

        Map<String, Object> stringObjectMap = list.get(0);
        long id = (long) stringObjectMap.get("id");
        String name = (String) stringObjectMap.get("name");
        String education = (String) stringObjectMap.get("education");

        // 打印出这条记录
        System.out.println("====>>> id: " + id + " name: " + name + " education: " + education);

        // 更改状态
        String updateSql = "update schedule_resume set state = '已归档' where id = ?";
        JdbcUtil.executeUpdate(updateSql, id);

        // 归档这条记录
        String insertSql = "insert into schedule_resume_bk select * from schedule_resume where id = ?";
        JdbcUtil.executeUpdate(insertSql, id);
    }
}
